home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdio / fwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-29  |  2.4 KB  |  74 lines

  1. /* 
  2.  * fwrite.c --
  3.  *
  4.  *    Source code for the "fwrite" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdio/RCS/fwrite.c,v 1.2 91/05/29 16:49:45 shirriff Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include "stdio.h"
  21.  
  22. /*
  23.  *----------------------------------------------------------------------
  24.  *
  25.  * fwrite --
  26.  *
  27.  *    This procedure outputs binary data to a buffered stream.
  28.  *
  29.  * Results:
  30.  *    The return value is the number of complete items actually written.
  31.  *    It may be less than numItems if an error condition was encountered;
  32.  *    in this case, there may be an additional partial item output after
  33.  *    the complete items.
  34.  *
  35.  * Side effects:
  36.  *    Up to numItems*size bytes are written into the stream from memory at
  37.  *    buf.
  38.  *
  39.  *----------------------------------------------------------------------
  40.  */
  41.  
  42. int
  43. fwrite(bufferPtr, size, numItems, stream)
  44.     register char *bufferPtr;    /* Origin of items to be written on stream.
  45.                  * Must contain numItems*size bytes. */
  46.     int size;            /* Size of each item to be written. */
  47.     int numItems;        /* Number of items to be written. */
  48.     register FILE *stream;    /* Stream where bytes are to be written. */
  49. {
  50.  
  51.     register int num, byteCount, itemCount;
  52.  
  53.     for (itemCount = 0; itemCount < numItems; itemCount++) {
  54.         for (byteCount = size; byteCount > 0;) {
  55.             if (stream->writeCount <=1 || stream->flags & STDIO_LINEBUF) {
  56.                 if (fputc(*bufferPtr, stream) == EOF) {
  57.                     return(itemCount);
  58.                 }
  59.                 bufferPtr++;
  60.                 byteCount--;
  61.             } else {
  62.                 num = stream->writeCount-1 < byteCount ? stream->writeCount-1
  63.                         : byteCount;
  64.                 bcopy(bufferPtr, stream->lastAccess+1, num);
  65.                 stream->writeCount -= num;
  66.                 stream->lastAccess += num;
  67.                 bufferPtr += num;
  68.                 byteCount -= num;
  69.             }
  70.         }
  71.     }
  72.     return(numItems);
  73. }
  74.